package com.feicai.aiagent.Advisor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.advisor.api.AdvisedRequest;
import org.springframework.ai.chat.client.advisor.api.AdvisedResponse;
import org.springframework.ai.chat.client.advisor.api.CallAroundAdvisor;
import org.springframework.ai.chat.client.advisor.api.CallAroundAdvisorChain;

import java.util.HashMap;

/**
 * 自定义Re2 Advisor
 * 可提高大语言模型的推理能力
 */
@Slf4j
public class Re_ReadAdvisor implements CallAroundAdvisor {
    public AdvisedRequest before(AdvisedRequest advisedRequest){
        HashMap<String, Object> stringObjectHashMap = new HashMap<>(advisedRequest.userParams());
        stringObjectHashMap.put("re_read", advisedRequest.userText());
        return AdvisedRequest.from(advisedRequest)
                .userText("""
                {re_read}
                Read the question again: {re_read}
                """)
                .userParams(stringObjectHashMap)
                .build();

    }

    @Override
    public AdvisedResponse aroundCall(AdvisedRequest advisedRequest, CallAroundAdvisorChain chain) {
        AdvisedRequest before = this.before(advisedRequest);
       return chain.nextAroundCall(before);
    }

    @Override
    public String getName() {
        return Re_ReadAdvisor.class.getName();
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
